package com.lyj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.lyj.dao.SubjectDao;
import com.lyj.pojo.Subject;
import com.lyj.pojo.SubjectQueryModel;
import com.lyj.util.base.BaseDaoImpl;
import com.lyj.util.base.BaseQueryModel;
import com.lyj.util.dao.DbHelper;

/**
 * 
 * @author 李艺江
 * @version 创建时间：2018年12月12日 上午11:08:44
 */

public class SubjectDaoImpl extends BaseDaoImpl implements SubjectDao {

	@Override
	public String getInsertSql(Object data) {
		Subject subject = (Subject) data;
		return "insert into t_subject(title,number,startTime,endTime,userId) " + " values('" + subject.getTitle()
				+ "','" + subject.getNumber() + "'," + subject.getStartTime() + "," + subject.getEndTime() + ","
				+ subject.getUser().getId() + ")";
	}

	@Override
	public String getUpdateSql(Object data) {
		Subject subject = (Subject) data;
		return "update t_subject set title='" + subject.getTitle() + "',number=" + subject.getNumber() + ",startTime="
				+ subject.getStartTime() + ",endTime=" + subject.getEndTime() + " where id=" + subject.getId();
	}

	@Override
	public String getDeleteSql(int id) {
		return "delete from t_subject where id="+id;
	}

	@Override
	public String getFindAllSql() {
		return "select * from t_subject";
	}

	@Override
	public String getFindConditionSql(BaseQueryModel queryModel) {
		SubjectQueryModel sq = (SubjectQueryModel) queryModel;
		StringBuilder sb=new StringBuilder();
		sb.append("select * from t_subject ");
		sb.append(" where 1=1 ");
		if(sq.getId() != null && sq.getId() > 0) {
			sb.append(" and id=" + sq.getId());
		}
		if(sq.getTitle() != null && sq.getTitle().trim().length() > 0) {
			sb.append(" and title='" + sq.getTitle() + "'");
		}if(sq.getNumber() > 0) {
			sb.append(" and numer=" + sq.getNumber());
		}if(sq.getStartTime() != null && sq.getStartTime() > 0) {
			sb.append(" and starttime=" + sq.getStartTime());
		}if(sq.getEndTime() != null && sq.getEndTime() > 0) {
			sb.append(" and endtime=" + sq.getEndTime());
		}if(sq.getUser().getId() != null && sq.getUser().getId() > 0) {
			sb.append(" and userId=" + sq.getUser().getId() );
		}
		return sb.toString();
	}

	@Override
	public Object rsToObject(ResultSet rs) throws Exception {
		// 将一条记录转换成一个实体类对象
		Subject subject = new Subject();
		// 将rs中记录的字段值读取出来
		// 赋值给实体类对象的属性
		subject.setId(rs.getLong("id"));
		subject.setTitle(rs.getString("title"));
		subject.setNumber(rs.getInt("number"));
		subject.setStartTime(rs.getLong("startTime"));
		subject.setEndTime(rs.getLong("endTime"));
		subject.getUser().setId((int) rs.getLong("userId"));
		return subject;
	}

	@Override
	public int getUserCount(Long subjectId) throws Exception {
		int result = 0;
		Connection con = DbHelper.getConnection();
		String sql = "SELECT COUNT(DISTINCT userId) as cnt FROM t_record WHERE subjectId=" + subjectId;
		PreparedStatement pst = con.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		if (rs.next()) {
			result = rs.getInt("cnt");
		}
		DbHelper.closeAll(null, pst, rs);
		return result;
	}
}
